@namespace Oqtane.Modules.Admin.Modules
@inherits ModuleBase
@inject NavigationManager NavigationManager
@inject IModuleService ModuleService
@inject IStringLocalizer<Export> Localizer
@inject IStringLocalizer<SharedResources> SharedLocalizer

<TabStrip>
    <TabPanel Name="Content" Heading="Content" ResourceKey="Content">
        <div class="container">
            <div class="row mb-1 align-items-center">
                <Label Class="col-sm-3" For="content" HelpText="Select the Export option and you will be able to view the module content" ResourceKey="Content">Content: </Label>
                <div class="col-sm-9">
                    <textarea id="content" class="form-control" @bind="@_content" rows="5" readonly></textarea>
                </div>
            </div>
        </div>
        <br />
        <button type="button" class="btn btn-success" @onclick="ExportText">@Localizer["Export"]</button>
        <NavLink class="btn btn-secondary" href="@PageState.ReturnUrl">@SharedLocalizer["Cancel"]</NavLink>
    </TabPanel>
    <TabPanel Name="File" Heading="File" ResourceKey="File">
        <div class="container">
            <div class="row mb-1 align-items-center">
                <Label Class="col-sm-3" For="folder" HelpText="Select a folder where you wish to save the exported content" ResourceKey="Folder">Folder: </Label>
                <div class="col-sm-9">
                    <FileManager ShowFiles="false" ShowUpload="false" @ref="_filemanager" />
                </div>
            </div>
            <div class="row mb-1 align-items-center">
                <Label Class="col-sm-3" For="filename" HelpText="Specify a name for the file (without an extension)" ResourceKey="Filename">Filename: </Label>
                <div class="col-sm-9">
                    <input id="content" type="text" class="form-control" @bind="@_filename" />
                </div>
            </div>
        </div>
        <br />
        <button type="button" class="btn btn-success" @onclick="ExportFile">@Localizer["Export"]</button>
        <NavLink class="btn btn-secondary" href="@PageState.ReturnUrl">@SharedLocalizer["Cancel"]</NavLink>
    </TabPanel>
</TabStrip>



@code {
    private string _content = string.Empty;
    private FileManager _filemanager;
    private string _filename = string.Empty;

    public override SecurityAccessLevel SecurityAccessLevel => SecurityAccessLevel.Edit;
    public override string Title => "Export Content";

    protected override void OnInitialized()
    {
        _filename = Utilities.GetFriendlyUrl(ModuleState.Title);
    }

    private async Task ExportText()
    {
        try
        {
            _content = await ModuleService.ExportModuleAsync(ModuleState.ModuleId, PageState.Page.PageId);
            AddModuleMessage(Localizer["Success.Content.Export"], MessageType.Success);
        }
        catch (Exception ex)
        {
            await logger.LogError(ex, "Error Exporting Module {ModuleId} {Error}", ModuleState.ModuleId, ex.Message);
            AddModuleMessage(Localizer["Error.Module.Export"], MessageType.Error);
        }
    }

    private async Task ExportFile()
    {
        try
        {
            var folderid = _filemanager.GetFolderId();
            if (folderid != -1 && !string.IsNullOrEmpty(_filename))
            {
                var fileid = await ModuleService.ExportModuleAsync(ModuleState.ModuleId, PageState.Page.PageId, folderid, _filename);
                if (fileid != -1)
                {
                    AddModuleMessage(Localizer["Success.Content.Export"], MessageType.Success);
                }
                else
                {
                    AddModuleMessage(Localizer["Error.Module.Export"], MessageType.Error);
                }
            }
            else
            {
                AddModuleMessage(Localizer["Message.Content.Export"], MessageType.Warning);
            }
        }
        catch (Exception ex)
        {
            await logger.LogError(ex, "Error Exporting Module {ModuleId} {Error}", ModuleState.ModuleId, ex.Message);
            AddModuleMessage(Localizer["Error.Module.Export"], MessageType.Error);
        }
    }

}
